package com.zhisheng.sql.blink.stream.example.websocket;

import com.google.gson.Gson;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Supplier;

public class PathBasedSocketServer extends WebSocketServer {
    private static class TimeNumber{
        private Date date;
        private int value;
        private int id;

        public Date getDate() {
            return date;
        }

        public void setDate(Date date) {
            this.date = date;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }

    // 路径配置数据结构
    private static class PathConfig {
        private String path;
        final Set<WebSocket> clients = new CopyOnWriteArraySet<>();

    }

    // 路径配置映射表
    private final Map<String, PathConfig> pathConfigs = new ConcurrentHashMap<>();

    public PathBasedSocketServer(int port) throws UnknownHostException {
        super(new InetSocketAddress(port));
        // 初始化路径配置
        //initializePaths();
    }



    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        String path = handshake.getResourceDescriptor();
        PathConfig config = pathConfigs.get(path);

        if (config == null) {
            PathConfig pathConfig = new PathConfig();
            pathConfig.path = path;
            pathConfig.clients.add(conn);
            pathConfigs.put(path,pathConfig);
        }else {
            config.clients.add(conn);
        }
        System.out.println("-------------------------------");
        System.out.println(path);
        System.out.println("-------------------------------");
        System.out.println("New connection to path: " + path);
    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        pathConfigs.values().forEach(config -> 
            config.clients.remove(conn)
        );
    }

    private void broadcastData(String path, String message) {
        PathConfig config = pathConfigs.get(path);
        if (config == null) return;

        config.clients.forEach(conn -> {
            if (conn.isOpen()) {
                conn.send(message);
            }
        });
    }

    public void sendMessage(Object value,String path){
        PathConfig config = pathConfigs.get(path);
        if (config != null){
            broadcastData(path,value.toString());
        }

    }

    @Override
    public void onMessage(WebSocket conn, String message) {
        // 根据需求处理客户端消息
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {
        ex.printStackTrace();
    }

    @Override
    public void onStart() {
        System.out.println("Server started on port: " + getPort());
    }

    public static void main(String[] args) throws UnknownHostException {
        PathBasedSocketServer server = new PathBasedSocketServer(9000);
        server.start();
    }

}